/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto;

import java.awt.Dimension;
import javax.swing.JOptionPane;
import projeto.classes.ModeloLogin;


public class Login extends javax.swing.JInternalFrame {

    private br.com.fatecpg.mdi.MDI2 mdi;
    private ModeloLogin mLog;
    
    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
    }
    
    public Login(br.com.fatecpg.mdi.MDI2 mdi) {
        this.mdi = mdi;
        mLog = new ModeloLogin();
        initComponents();
    }
    
    //metodo para centralizar janela ao abri-la
    public void setPosicao() {  
        Dimension d = this.getDesktopPane().getSize();  
        this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2); 
    }
  

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnLogar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        txtUsuario = new javax.swing.JTextField();
        txtSenha = new javax.swing.JPasswordField();

        setClosable(true);
        setTitle("Entrega gado - Login");

        jLabel1.setText("Usuário");

        jLabel2.setText("Senha");

        btnLogar.setText("Logar");
        btnLogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(btnLogar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed

        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLogarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogarActionPerformed
        String usuario = (txtUsuario.getText().isEmpty()) ? "" : txtUsuario.getText();
        String senha = (txtSenha.getPassword().length > 0) ? new String(txtSenha.getPassword()) : "";
        
        if (usuario.isEmpty() && senha.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Informe seu usuário e senha", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } else if (usuario.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Usuário é obrigatório", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } else if (senha.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Senha é obrigatória", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } else {
            try {
            projeto.classes.ModeloUsuario usr = mLog.Logar(usuario, senha);
            
            if (usr != null) {
                JOptionPane.showMessageDialog(this, "Usuário " + usr.getNome() + " logado", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                mdi.setUsuarioLogado(usr);
                mdi.getMenuPrincipal().setEnabled(true);
                mdi.getDeslogarMenuItem().setEnabled(true);
                mdi.getLogarMenuItem().setEnabled(false);
                mdi.getLblUsuarioLogado().setText("Usuário logado: " + usr.getNome());
                if (usr.getUsuarioAdmin() == 0) {
                    mdi.getUsuarioMenuItem().setVisible(false);
                    mdi.getTipo_AnimalMenuItem().setVisible(false);
                    mdi.getTipo_DespesaMenuItem().setVisible(false);
                    mdi.getTipo_VeiculoMenuItem().setVisible(false);
                } else {
                    mdi.getUsuarioMenuItem().setVisible(true);
                }
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Usuário ou senha incorretos", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Erro:\n" + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
        }
        }
        
        
    }//GEN-LAST:event_btnLogarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnLogar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField txtSenha;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
